﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Survey : Page
{
    public int PageSize
    {
        get { return 8; }
    }

    private List<Answer> answers = new List<Answer>();

    protected void Page_Load(object sender, EventArgs e)
    {
        SecureSession ss = new SecureSession();
        if (ViewState[FormContract.CONSENT_STATUS] == null)
            ViewState[FormContract.CONSENT_STATUS] = ss.ConsentStatus;
        else
            Session[FormContract.CONSENT_STATUS] = ViewState[FormContract.CONSENT_STATUS];

        if(ViewState[FormContract.USER_REF] == null && Session[FormContract.USER_REF] == null)
            Response.Redirect(ConfigurationManager.AppSettings["RedirectUrl"].ToString());            

        if(ViewState[FormContract.USER_REF] == null)
            ViewState[FormContract.USER_REF] = Session[FormContract.USER_REF];
        IDbConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["SurveyConnectionString"].ConnectionString);
        using (conn)
        {
            conn.Open();
            PageBuilder pb = new PageBuilder(PageSize);
            IEnumerable<Part> list = Part.Load(conn);
            Session["parts"] = list;
            PlaceHolder1.Controls.Add(pb.GetPages(list));            
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        string sql_script2 =
@"
INSERT INTO [Answers](question_id,participant_id,mark) VALUES (@question_id,@participant_id,@mark)
";
        IDbConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["SurveyConnectionString"].ConnectionString);
        //int new_pant_id = (int)Session["new_pant_id"];
        Participant participant = (Participant)ViewState[FormContract.USER_REF];
        int new_pant_id = participant.ParticipantID;
        using (conn)
        {
            conn.Open();
            IDbTransaction tran = conn.BeginTransaction();
            try
            {
                IDbCommand cmd = conn.CreateCommand();
                //cmd.Parameters.Clear();
                cmd.CommandText = sql_script2;
                IDbDataParameter question_param = cmd.CreateParameter();
                IDbDataParameter participant_param = cmd.CreateParameter();
                IDbDataParameter mark_param = cmd.CreateParameter();
                question_param.DbType = DbType.Int32;
                question_param.ParameterName = "@question_id";
                participant_param.DbType = DbType.Int32;
                participant_param.ParameterName = "@participant_id";
                participant_param.Value = new_pant_id;
                mark_param.DbType = DbType.Int64;
                mark_param.ParameterName = "@mark";
                cmd.Parameters.Add(question_param);
                cmd.Parameters.Add(participant_param);
                cmd.Parameters.Add(mark_param);
                cmd.Transaction = tran;
                foreach (string key in Request.Form.AllKeys)
                {
                    if (FormContract.IsQuestion(key))
                    {
                        int id = FormContract.GetQuestionID(key);
                        int value = Convert.ToInt32(Request[key]);
                        question_param.Value = id;
                        mark_param.Value = value;
                        cmd.ExecuteNonQuery();
                        Answer ans = new Answer();
                        ans.Number = id;
                        ans.Ans = Request[key];
                        answers.Add(ans);
                    }
                }
                Session["answers"] = answers;
                tran.Commit();
                Session[FormContract.USER_REF] = ViewState[FormContract.USER_REF];
                Response.Redirect("report.aspx?id=" + new_pant_id, false);                
            }
            catch (Exception error)
            {
                tran.Rollback();
                throw (error);
            }
        }
    }
}
